package util;

import java.io.*;
import java.nio.charset.Charset;
import java.util.LinkedList;

/**
 * PackageName: util
 * ClassName: FileOperator
 * Author: tum0r
 * Time: 2020/5/23 16:34
 */
public class FileOperator {

    private LinkedList<String> fileList = new LinkedList<>();

    public LinkedList<String> getAllFiles(String path) {
        fileList.clear();
        getFiles(new File(path));
        return fileList;
    }

    private void getFiles(File file) {
        if (file != null && file.exists() && file.canRead() && file.canWrite()) {
            if (file.isDirectory()) {
                File[] children = file.listFiles();
                for (File child : children) {
                    getFiles(child);
                }
            } else if (file.isFile()) {
                fileList.add(file.getAbsolutePath());
            }
        }
    }

    public void writeFile(String path, String data) throws IOException {
        writeFile(new File(path), data, true);
    }

    public void writeFile(String path, String data, boolean append) throws IOException {
        writeFile(new File(path), data, append);
    }

    public void writeFile(File file, String data, boolean append) throws IOException {
        if (file != null) {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file, append);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, Charset.defaultCharset());
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStreamWriter.close();
            fileOutputStream.close();
        }
    }

    public boolean writeFile(String path, byte[] data) throws IOException {
        return writeFile(new File(path), data, true);
    }

    public boolean writeFile(String path, byte[] data, boolean append) throws IOException {
        return writeFile(new File(path), data, append);
    }

    public boolean writeFile(File file, byte[] data, boolean append) throws IOException {
        boolean result = false;
        if (file != null && data != null && data.length != 0) {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file, append);
            fileOutputStream.write(data);
            fileOutputStream.flush();
            fileOutputStream.close();
            result = true;
        }
        return result;
    }

    public byte[][] readFileBytes(String path) throws IOException {
        return readFileBytes(new File(path));
    }

    public byte[][] readFileBytes(File file) throws IOException {
        byte[][] result = new byte[0][];
        if (file != null && file.isFile() && file.canRead()) {
            long fileSize = file.length();
            int size = 268435456;
            int count = (int) (fileSize / size);
            if (fileSize % size != 0) {
                count++;
            }
            result = new byte[count][];
            byte[] temp = new byte[size];
            FileInputStream fileInputStream = new FileInputStream(file);
            int index = 0;
            while ((count = fileInputStream.read(temp)) != -1) {
                result[index] = new byte[count];
                System.arraycopy(temp, 0, result[index++], 0, count);
            }
            fileInputStream.close();
        }
        return result;
    }

    public LinkedList<String> readLine(String path) throws IOException {
        return readLine(new File(path));
    }

    public LinkedList<String> readLine(File file) throws IOException {
        LinkedList<String> result = new LinkedList<>();
        if (file != null && file.isFile() && file.canRead()) {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                result.add(line);
            }
            bufferedReader.close();
            inputStreamReader.close();
            fileInputStream.close();
        }
        return result;
    }

    public void deleteFile(String path) {
        deleteFile(new File(path));
    }

    public void deleteFile(File file) {
        if (file != null && file.isFile()) {
            file.delete();
        }
    }
}
